<template>
  <!--  登录表单容器-->
  <div class="login_container">
    <!--登录区域-->
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../assets/img/timg.gif" alt="">
      </div>
      <!-- 表单-->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login_form">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="verifyCode">
          <div class="verifyCode_box">
            <el-input v-model="loginForm.validateCode" placeholder="请输入验证码" prefix-icon="el-icon-mobile"
                      class="verifyCode"></el-input>
            <img :src="image" alt="" class="verifyCode_img">
          </div>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import { test1 } from '../api/test'
  export default {
    name: 'Login',
    data () {
      return {
        loginForm: {
          username: 'xiaoge',
          password: 'admin',
          validateCode: '',
          token: 'aaaaa',
        },
        loginRules: {
          username: [
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 8,
              message: '长度在 3 到 8 个字符',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 8,
              message: '长度在 3 到 8 个字符',
              trigger: 'blur'
            }
          ],
          validateCode: [
            {
              required: true,
              message: '请输入验证码',
              trigger: 'blur'
            }
          ],
        },
        /*验证码图片*/
        image: '',
      }
    },
    methods: {
      // 点击登录按钮触发的登录事件
      submitForm (formName) {
        // 验证参数有效性
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 通过qs处理参数，否则后端拿数据不到
            let param = this.$qs.stringify({
              username: this.loginForm.username,
              password: this.loginForm.password,
              token: this.loginForm.token,
              validateCode: this.loginForm.validateCode,
            })
            // 发送登录请求
            this.$http.post('/login', param
            ).then((res) => {
              // 判断是否登录成功
              if (res.data.success === true) {
                console.log(res);
                // 给出登录成功信息
                this.$message({
                  message: '登录成功',
                  type: 'success'
                });
                // 存放认证信息
                this.$store.dispatch('changeAuthorization',res.headers.authorization);

                window.localStorage.setItem('authorization', JSON.stringify(res.headers.authorization));
                // console.log(window.localStorage.getItem('authorization'));
                // localStorage.setItem('userName', this.loginForm.userName)
                //获取并存储服务器返回的AuthorizationToken信息
                // let authorization = successResponse.headers['authorization']
                // console.log(authorization);
                // localStorage.setItem('authorization', authorization)
                // 跳转页面
                this.$router.push('/main',this.loginForm.username)
              }
            }).catch(function (error) {
              console.log(error)
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
      },
      async getCaptchaImg () {
        const { data } = await test1()
        // 获取验证码图片base64码
        this.image = data.data.captchaImg
        // 获取token
        this.loginForm.token = data.data.token
      },
    },
    created () {
      this.getCaptchaImg()
    }
  }
</script>

<style scoped>
  .login_container {
    height: 100%;
    background-color: aquamarine;
  }

  .login_box {
    width: 450px;
    height: 380px;
    background-color: #FFFFFF;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .login_box .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin: -65px auto;
    background-color: #FFFFFF;
  }

  .login_box .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
  }

  .login_form {
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0px 20px;
    box-sizing: border-box;
  }

  .login_btn {
    display: flex;
    justify-content: flex-end;
  }

  .verifyCode_box {
    display: flex;
  }

  .verifyCode {
    width: 70%;
    justify-content: left;
  }

  .verifyCode_img {
    width: 30%;
    margin-left: 10px;
    height: 40px;
    justify-content: flex-end;
  }
</style>
